/*
 * @Descripttion: 
 * @Author: xiaodong
 * @Date: 2020-11-23 10:03:07
 * @LastEditTime: 2021-02-06 16:26:14
 * @FilePath: \cloud-monitor-sdk\src\utils\index.ts
 */
import { Config } from "../config";
import { randomString, parseHash } from "./tools";
import { GlobalVal } from "../config/global";

export function getCommonMsg() {
    //返回网络连接状态NetworkInformation对象，包括.downlink（网络下行速度）  
    //effectiveType（网络类型） onchange（有值代表网络状态变更） rtt（估算的往返时间） saveData（打开/请求数据保护模式）
    //存在浏览器兼容
    let connectionInfo = (navigator as any).connection;
    let data: CommonMsg = {
        type: "",//
        page: getPage(),
        times: 1,
        version: Config.appVersion,
        token: Config.token,
        environment: Config.environment,// 环境
        beginTime: new Date().getTime(),
        uid: getUid(),
        sid: GlobalVal.sid,
        screen: screen.width + "x" + screen.height,
        viewScreen: getScreen(),
        // connectionType: connectionInfo ? connectionInfo.effectiveType : "",//网络类型
        // lang: getLang(),
        // sdkVersion: "{{VERSION}}",
        url: location.href,
    };
    return data;
}

// 获取页面
function getPage(): string {
    if (GlobalVal.page) return GlobalVal.page;
    else {
        return location.pathname.toLowerCase();
    }
}

// 获取uid
function getUid(): string {
    let uid = localStorage.getItem("sdk_uid") || "";
    if (!uid) {
        uid = randomString();
        localStorage.setItem("sdk_uid", uid);
    }
    return uid;
}

// 获得sid
// TODO: 单页面
// function getSid() {
//   const date = new Date();
//   let sid = sessionStorage.getItem('bombay_sid') || '';
//   if (!sid) {
//       sid = randomString();
//       sessionStorage.setItem('bombay_sid', sid);
//   }
//   return sid;
// }

// 获取浏览器默认语言
function getLang() {
    var lang = navigator.language || (navigator as any).userLanguage; //常规浏览器语言和IE浏览器
    lang = lang.substr(0, 2); //截取lang前2位字符
    return lang;
}
// 可见区域分辨率
function getScreen() {
    let w = document.documentElement.clientWidth || document.body.clientWidth;
    let h = document.documentElement.clientHeight || document.body.clientHeight;
    return w + "x" + h;
}
